define(function (require, exports, module) {
    const Constants = require('./utils/constants.js').onload();
    const CommonSelectors = require('./utils/common_selectors.js').onload();

    function onload() {
        const DrawCenter = {};
        const DrawCenterEvents = Object.assign({
            UPDATE_POINT_CENTER: 'draw.update.point.center'
        }, Constants.events)

        DrawCenter.onSetup = function() {
            const point = this.newFeature({
                type: Constants.geojsonTypes.FEATURE,
                properties: {},
                geometry: {
                    type: Constants.geojsonTypes.POINT,
                    coordinates: []
                }
            });

            this.addFeature(point);

            this.clearSelectedFeatures();
            this.updateUIClasses({ mouse: Constants.cursors.ADD });
            this.activateUIButton(Constants.types.POINT);

            this.setActionableState({
                trash: true
            });

            return { point };
        };

        DrawCenter.stopDrawingAndRemove = function(state) {
            this.deleteFeature([state.point.id], { silent: true });
            this.changeMode(Constants.modes.SIMPLE_SELECT);
        };

        DrawCenter.onTap = DrawCenter.onClick = function(state, e) {
            this.updateUIClasses({ mouse: Constants.cursors.MOVE });
            state.point.updateCoordinate('', e.lngLat.lng, e.lngLat.lat);
            this.map.fire(DrawCenterEvents.UPDATE_POINT_CENTER, {
                features: [state.point.toGeoJSON()]
            });
            this.changeMode(Constants.modes.SIMPLE_SELECT, { featureIds: [state.point.id] });
        };

        DrawCenter.onStop = function(state) {
            this.activateUIButton();
            if (!state.point.getCoordinate().length) {
                this.deleteFeature([state.point.id], { silent: true });
            }
        };

        DrawCenter.toDisplayFeatures = function(state, geojson, display) {
            // Never render the point we're drawing
            const isActivePoint = geojson.properties.id === state.point.id;
            geojson.properties.active = (isActivePoint) ? Constants.activeStates.ACTIVE : Constants.activeStates.INACTIVE;
            if (!isActivePoint) return display(geojson);
        };

        DrawCenter.onTrash = DrawCenter.stopDrawingAndRemove;

        DrawCenter.onKeyUp = function(state, e) {
            if (CommonSelectors.isEscapeKey(e) || CommonSelectors.isEnterKey(e)) {
                return this.stopDrawingAndRemove(state, e);
            }
        };

        return {
            DrawCenter,
            DrawCenterEvents
        }
    }

    module.exports = {
        onload
    }
});
